package com.linser.gistools.utils;

import com.linser.gistools.utils.enums.NotificationType;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextInputDialog;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.controlsfx.control.Notifications;
import org.controlsfx.dialog.ExceptionDialog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Optional;

/**
 * @author: linser
 * @Desc: 弹窗工具类
 * @create: 2024-02-02 20:01
 **/
public class DialogUtils {

    private static final Logger logger = LoggerFactory.getLogger(DialogUtils.class);
    /**
     * show error dialog
     *
     * @param title     header title of dialog
     * @param throwable error info
     */
    public static void showErrorDialog(Throwable throwable, String title) {
        throwable.printStackTrace();
        Platform.runLater(() -> {
            ExceptionDialog exceptionDialog = new ExceptionDialog(throwable);
            DialogPane pane = exceptionDialog.getDialogPane();
//            pane.getStylesheets().add("css/base.css");
            exceptionDialog.setHeaderText(title);
            exceptionDialog.setTitle("Exception detail");
            exceptionDialog.show();
        });
    }

    /**
     * show notification
     *
     * @param text notification content
     * @param pos  {@link Pos} notification show position
     * @param type {@link NotificationType} notification type
     */
    public static void showNotification(String text, Pos pos, NotificationType type) {
        Platform.runLater(() -> {
            Notifications notifications = Notifications.create();
            notifications.position(pos);
            notifications.text(text);
            switch (type) {
                case ERROR:
                    notifications.showError();
                    break;
                case WARNING:
                    notifications.showWarning();
                    break;
                case INFORMATION:
                    notifications.showInformation();
                    break;
                case CONFIRMATION:
                    notifications.showConfirm();
                    break;
                default:
                    notifications.show();
            }
        });
    }

    /**
     * show confirm message
     *
     * @param message message content
     * @return <p>return the confirmation result.If you click OK,
     * it will return true. Otherwise, it will return false<p/>
     */
    public static boolean showAlertConfirm(String message) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setContentText(message);
//        alert.getDialogPane().getStylesheets().add("css/base.css");
        Optional<ButtonType> optional = alert.showAndWait();
        return optional.isPresent() && optional.get() == ButtonType.OK;
    }

    /**
     * Display the input dialog box and return the input results
     *
     * @param title dialog title
     * @return inout title
     */
    public static String showInputDialog(String title) {
        TextInputDialog dialog = new TextInputDialog();
        dialog.setTitle("inout dialog");
        dialog.setHeaderText(title);
//        dialog.getDialogPane().getStylesheets().add("css/base.css");
        Optional<String> optional = dialog.showAndWait();
        return (String) optional.orElse("");
    }

    /**
     * show dialog message
     *
     * @param message message content
     */
    public static void showAlertInfo(String message) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("消息");
        alert.setContentText(message);
//        alert.getDialogPane().getStylesheets().add("css/base.css");
        alert.showAndWait();
    }


    /**
     * @param loader fxml加载器
     * @param stageTitle Stage标题
     * @return Stage
     */
    public static Stage showFxmlStage(FXMLLoader loader, String stageTitle) throws IOException {
        Parent root = loader.load();
        Scene scene = new Scene(root);
        Stage stage = new Stage();
        stage.setTitle(stageTitle);
        stage.getIcons().add(GlobalVariables.logoIcon);
        //stage.initOwner(GlobalVariables.mainStage);
        //表示弹窗具有应用程序级别的模态性
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initStyle(StageStyle.DECORATED);
        stage.setResizable(true);
        stage.setScene(scene);
        stage.showAndWait();
        return stage;
    }
}
